
/*
 * Desenvolvimento para WEB
 * 
 * NOME: Vitor Augusto Graciani           RA: 345270
 * NOME: Felipe Petruz Brina              RA: 345083
 * NOME: Diego Zamboni Latance            RA: 344990
 * NOME: Rafael Claudio Domene            RA: 345040
 * NOME: Vinicius Rodrigues de Campos     RA: 345350
 * 
 */


package model;

import auxiliar.ConnectionFactory;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class ValidaLoginDAO {

    private Connection conn;

    public ValidaLoginDAO() throws DAOException {
        try {
            this.conn = ConnectionFactory.getConnection();

        } catch (Exception e) {
            throw new DAOException("Erro: " + ":\n" + e.getMessage());
        }
    }

    public UsuarioBEAN recuperaDados(UsuarioBEAN usuarioComparacao) throws DAOException {

        PreparedStatement ps = null;
        Connection conn = null;
        ResultSet rs = null;
        UsuarioBEAN novo = null;

        try {

            String SQL = "SELECT email, senha FROM colaborador";
            conn = this.conn;
            ps = conn.prepareStatement(SQL);
            rs = ps.executeQuery();
            while (rs.next()) {
                String login = rs.getObject("email").toString();
                String senha = rs.getObject("senha").toString();
                if (login.equals(usuarioComparacao.getEmail()) && senha.equals(usuarioComparacao.getSenha())) {
                    novo = new UsuarioBEAN(null, login, senha);
                }
            }
            return novo;

        } catch (SQLException sqle) {
            throw new DAOException(sqle);
        } finally {
            ConnectionFactory.closeConnection(conn, ps, rs);
        }


    }
}